<?php
class FileModel extends Model {
	protected $_validate = array();
	public function addFile($info, $id, $module) {
		foreach ($info as $val) {
			$data[] = array('module_id' => $id, 'module' => $module, 'url' => $val['savepath'] . $val['savename'], 'name' => $val['name'], 'type' => $val['type'], 'size' => $val['size'], 'create_time' => time());
		}
		return M('file') -> addAll($data);
	}

	public function deteleFile($file_id, $id, $module) {
		$where['module_id'] = $id;
		$where['module'] = $module;
		$where['file_id'] = is_array($file_id) ? array('in', $file_id) : $file_id;
		$url = M('file') -> where($where) -> getField('url');
		if ( M('file') -> where($where) -> delete()) {
			@unlink($url);
			return true;
		} else {
			return false;
		}
	}

	public function output($status) {
		$m = M("user");
		$str = D('Structure');
		$user = D('User') -> getOneuser($_GET['uname']);
		$ver = D('Verify');

		$users = $m -> where(array("status" => $status)) -> select();
		$usersed = array();

		foreach ($users as $k => $user) {
			//查上级
			$gid['dep'] = $str -> getTopposition($user['department_id']);
			foreach ($gid['dep']['id'] as $key => $vv) {
				$dname[$key] = $str -> getDename($vv);
				$user[$k]['dep'] .= ' - ' . $dname[$key];
			}

			$usersed[$k]['id'] = $users[$k]['user_id'];
			$usersed[$k]['nickname'] = $users[$k]['nickname'];
			$usersed[$k]['dep'] = substr($user[$k]['dep'], 2);
			$usersed[$k]['pos'] = $str -> getPosition_n($user["position_id"]);
			$usersed[$k]["telephone"] = $users[$k]["telephone"];
			if ($users[$k]["bankcard"] == '') {
				$users[$k]["bankcard"] = "未设置";
			} else {
				$usersed[$k]["bankcard"] = $users[$k]["bankcard"];
			}
			if ($users[$k]["sfzid"] == '') {
				$usersed[$k]["sfzid"] = "未设置";
			} else {
				$usersed[$k]["sfzid"] = $users[$k]["sfzid"];
			}
			$usersed[$k]['unline']=$users[$k]['unline'];
		}
		return $usersed;
	}

	/**
	 * 导出打卡记录
	 **/
	public function exportExcel($data,$titles) {
		import("ORG.PHPExcel.PHPExcel");
		$objPHPExcel = new PHPExcel();
		$objProps = $objPHPExcel -> getProperties();
		$objProps -> setCreator("5khrm");
		$objProps -> setLastModifiedBy("5khrm");
		$objProps -> setTitle("5khrm Punch Data");
		$objProps -> setSubject("5khrm Punch Data");
		$objProps -> setDescription("5khrm Punch Data");
		$objProps -> setKeywords("5khrm Punch Data");
		$objProps -> setCategory("Punch");
		$objPHPExcel -> setActiveSheetIndex(0);
		$objActSheet = $objPHPExcel -> getActiveSheet();

		$objActSheet -> setTitle('可用可信'.$titles.'职员信息表');
		$objActSheet -> setCellValue('A1', 'ID');
		$objActSheet -> setCellValue('B1', '姓名');
		$objActSheet -> setCellValue('C1', '部门');
		$objActSheet -> setCellValue('D1', '职位');
		$objActSheet -> setCellValue('E1', '手机号码');
		$objActSheet -> setCellValue('F1', '银行卡号码');
		$objActSheet -> setCellValue('G1', '身份证');
		if($titles=="离职"){
			$objActSheet -> setCellValue('H1', '离职日期');
		}

		//		$list = $this -> getPunchAll();
		$i = 1;
		foreach ($data as $v) {
			$i++;
			$objActSheet -> setCellValue('A' . $i, $v["id"]);
			$objActSheet -> setCellValue('B' . $i, $v["nickname"]);
			$objActSheet -> setCellValue('C' . $i, $v["dep"]);
			$objActSheet -> setCellValue('D' . $i, $v["pos"]);
			$objActSheet -> setCellValueExplicit('E' . $i, $v["telephone"],PHPExcel_Cell_DataType::TYPE_STRING);
			$objActSheet -> setCellValueExplicit('F' . $i, $v["bankcard"],PHPExcel_Cell_DataType::TYPE_STRING);
			$objActSheet -> setCellValueExplicit('G' . $i, $v["sfzid"],PHPExcel_Cell_DataType::TYPE_STRING);
			if($titles=="离职"){
				if($v['unline']==''){
					$v['unline']='2016-08-30';
				}else{
					$v['unline']=date("Y-m-d",$v['unline']);
				}
				$objActSheet -> setCellValueExplicit('H' . $i, $v['unline'],PHPExcel_Cell_DataType::TYPE_STRING);
			}

			//			$objActSheet -> setCellValue('C' . $i, date('Y-m-d H:i:s', $v['create_time']));
			//			$v['type'] == 0 ? $objActSheet -> setCellValue('D' . $i, '上班打卡') : $objActSheet -> setCellValue('D' . $i, '下班打卡');
			//			$objActSheet -> setCellValue('E' . $i, $v['from_ip']);
		}
		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
		header("Content-Type: application/vnd.ms-excel;");
		header("Content-Disposition:attachment;filename=可用可信".$titles."职员信息_" . date('Y-m-d', mktime()) . ".xls");
		header("Pragma:no-cache");
		header("Expires:0");
		$objWriter -> save('php://output');
	}

}
